// 转义
function htmlDecode(str) {
  if (str.length === 0) {
    return ""
  } else {
    var s = str.replace(/&gt;/g, "&");
    s = s.replace(/&lt;/g, "<");
    s = s.replace(/&gt;/g, ">");
    s = s.replace(/&nbsp;/g, " ");
    s = s.replace(/&#39;/g, "\'");
    s = s.replace(/&quot;/g, "\"");
    s = s.replace(/<br>/g, "\n");
    return s;
  }
}
function htmlEncode(str) {
  if (str.length === 0) {
    return ""
  } else {
    var s = str.replace(/&/g, "&gt;");
    s = s.replace(/</g, "&lt;");
    s = s.replace(/>/g, "&gt;");
    s = s.replace(/ /g, "&nbsp;");
    s = s.replace(/\'/g, "&#39;");
    s = s.replace(/\"/g, "&quot;");
    s = s.replace(/\n/g, "<br>");
    return s;
  }
}

// 防抖
function debounce(fn, delay) {
  // 维护一个 timer
  var timer = null;
  return function() {
    // 通过 ‘this’ 和 ‘arguments’ 获取函数的作用域和变量
    var context = this;
    var args = arguments;
    clearTimeout(timer);
    timer = setTimeout(function() {
      fn.apply(context, args);
    }, delay);
  }
}
//节流
function throttle(func,delay){
  var prev = new Date().getTime();
  return function(){
    var context = this;
    var args = arguments;
    var now = new Date().getTime();
    if(now-prev>=delay){
      func.apply(context,args);
      prev = new Date().getTime();
    }
  }
}